<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item">
        <input type="hidden" id="id" name="id">
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">电影类型</label>
        <div class="layui-input-block">
            <input type="text" id="typeName" name="typeName" lay-verify="required" lay-reqtext="电影类型名不能为空" placeholder="请输入电影类型名" value="" onblur="check_movieType()" class="layui-input">
            <span style="color: red" id="error"></span>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认修改</button>
        </div>
    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>

<script>
    //判断电影名是否已存在
    function check_movieType(){
        let flag = true;
        $.ajax({
            type:'POST',
            url:'${path}/types/isExist',
            data:{"typeName":$("#typeName").val()},
            async:false,  //默认true是异步，要设置为false同步
            success:function (result){
                console.log(result.object);
                if(result.code == "200" ){
                    $("#error").text("");
                }else {
                    if(result.object==$("#id").val()){
                        $("#error").text("");
                    }else {
                        $("#error").text("电影类型名已存在！请换个吧！");
                        flag = false;
                        return false;
                    }
                }
            }
        });
        if(!flag){
            return false;
        }else {
            return true;
        }
    }

    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$;
        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let index = layer.confirm('确认是否修改？', {
                title: '修改电影类型',
                icon:'3'
            }, function (index) {
                if(!check_movieType())return false;
                $.post("${path}/types/editType", {"typeName":$("#typeName").val(),"id":$("#id").val()},function (result){
                    if(result.code=="200"){
                        layer.msg('修改成功！', {icon:1, time: 2000},function (){
                            parent.location.reload();
                            // 关闭弹出层
                            layer.close(index);
                            let iframeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(iframeIndex);
                        });
                    }else {
                        layer.msg('修改失败！', {icon:2});
                    }
                });
            });
            return false;
        });
    });

    function child(data){
        layui.use(['form'], function () {
            let form = layui.form,
                $ = layui.$;
            $("#id").val(data.id)
            $("#typeName").val(data.typeName)
            form.render();          //表单渲染
        });
    }


    layui.use(['form', 'layer', 'upload'], function () {
        let $ = layui.jquery;
        let form = layui.form,
            layer = layui.layer,
            upload = layui.upload;

        /*监听提交*/
        form.on('submit(add_recharge_submit)', function (data) {
            addRecharge(data.field);
            return false;
        });
    });
</script>
</body>
</html>